package 经典100题;

import java.util.HashMap;
import java.util.Map;

public class 无重复最长子串 {

    public static int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0){
            return 0;
        }
        if (s.length() == 1){
            return 1;
        }
        char[] charArrays = s.toCharArray();
        Map indexMap = new HashMap();
        int start = 0;
        int max = 0;
        int index = 0;
        for (int i = 0; i < charArrays.length; i++) {
            char c = charArrays[i];
            index = indexMap.get(c) != null ? (int)indexMap.get(c): 0;
            start = Math.max(index, start);
            max =  Math.max(max, i - start + 1);
            indexMap.put(c, i + 1);
        }
        return max;
    }

    public static void main(String[] args) {
        //"tmmzuxt","bbbbb"
        System.out.println(lengthOfLongestSubstring("bbbbb"));
    }
}
